//
//  ZCredpackOldVC.m
//  zctx
//
//  Created by apple on 16/1/22.
//  Copyright © 2016年 sanliang. All rights reserved.
//

#import "ZCredpackOldVC.h"
#import "ZCRedOldCell.h"
#import "ZCRedpackModelContent.h"
@interface ZCredpackOldVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UILabel *numLable;
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSArray *dataArray;

@end

@implementation ZCredpackOldVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ZCredpackOldVC"];
    [MTA trackPageViewBegin:@"ZCredpackOldVC"];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ZCredpackOldVC"];
    [MTA trackPageViewEnd:@"ZCredpackOldVC"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"历史红包";
    [self initnavegation];
    self.view.backgroundColor =[UIColor colorWithHexString:@"#ebeded"];
    self.dataArray = [[NSArray alloc]init];
    
    [self requestData];
    
    [self initTableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCRedOldCell" bundle:nil] forCellReuseIdentifier:@"redoldCell"];
}

#pragma mark 导航栏
-(void)initnavegation{
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
}

-(void)backButtonPressed{
    ProgressDismiss;
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 我的数据
-(void)requestData{
    [NetworkManager requestGetMyRedpackWithmid:UserModel.mid password:UserModel.password type:@"1" pagecount:@"0" rp_id:@"0" success:^(NSDictionary *dictResponse) {
        if ([[dictResponse objectForKey:@"content"] isKindOfClass:[NSArray class]]){
            NSArray *sarray = (NSArray *)[dictResponse objectForKey:@"content"];
            if (sarray.count>0) {
                self.dataArray = [sarray copy];
                [self.tableView reloadData];
            }
        }
    } failure:^(NSError *error) {
    }];
}

-(void)initTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.sectionFooterHeight = 0.0001;
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    self.tableView.contentInset = UIEdgeInsetsMake(20,0, 0, 0);
}
#pragma mark UITableViewDataSource,UITableViewDelegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIndentifier = @"redoldCell";
    ZCRedOldCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell == nil) {
        cell = [(ZCRedOldCell *)[ZCRedOldCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    ZCRedpackModelContent *dict =[ZCRedpackModelContent modelObjectWithDictionary:self.dataArray[indexPath.row]];
    cell.oldModel = dict;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0001;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (self.dataArray.count>0) {
        UIView *footView = [[UIView alloc]init];
        return footView;
    }else{
        UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH,400)];
        footView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
        
        UIImageView*redImg = [[UIImageView alloc]init];
        [footView addSubview:redImg];
        [redImg makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(footView.top).with.offset(0.255 * SCREEN_HEIGHT-44);
            make.centerX.equalTo (footView.centerX).with.offset(0);
            make.height.mas_equalTo(100);
            make.width.mas_equalTo(100);
        }];
        [redImg setImage:[UIImage imageNamed:@"me_red_money"]];
        
        
        UILabel*redLable = [[UILabel alloc]init];
        [footView addSubview:redLable];
        [redLable makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(redImg.bottom).with.offset(23);
            make.centerX.equalTo (footView.centerX).with.offset(0);
            make.height.mas_equalTo(21);
            make.width.mas_equalTo(150);
        }];
        redLable.textAlignment = NSTextAlignmentCenter;
        redLable.text = @"暂时无红包";
        redLable.textColor = [UIColor colorWithHexString:@"#d2d2d2"];
        redLable.font = [UIFont systemFontOfSize:16];
        return footView;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (self.dataArray.count>0) {
        return 0.0001;
    }else{
        return 400;
    }
}

@end
